<template>
  <div class="add_modle_dialog">
    <el-dialog title="角色权限设置" :visible.sync="visible" width="30%">
      <el-form :model="form" ref="form">
        <el-form-item label="名称：" :label-width="formLabelWidth" prop="roleName" :rules="[rules,rulesLength20]">
          <el-input v-model="form.roleName" auto-complete="off" placeholder="请输入" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="权限设置：" :label-width="formLabelWidth" prop="privilegeIds">
          <el-checkbox-group v-model="form.privilegeIds" @change="changePrivileges">
            <el-row>
              <el-col :span="12" v-for="(item,index) in privileges" :key="index" >
                <el-checkbox   :label="item.id" >{{item.name}}</el-checkbox>
              </el-col>
            </el-row>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="confirm('form')" v-if="!isedit">确 定</el-button>
        <el-button type="primary" @click="save('form')" v-if="isedit">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import ApiClient from '@/utils/ApiClient'
  import ValidataRules from '@/utils/validateRules'


  export default {
    name: 'add_charactor_dialog',
    props: {
      show: {type: Boolean, default: false},
      dataItem: {type: Object},
      isedit:{type:Boolean,default:false}
    },
    components: {
    },
    data() {
      return {
        formLabelWidth: '100px',
        form: {
          id:null,
          roleName: '',
          privilegeIds:[],
        },
        privileges:[],
        rules: {required: true, message: '不能为空', trigger: 'blur'},
        rulesLength20: { validator: ValidataRules.limitMaxLength(20), trigger: 'blur' }
      }
    },
    methods: {
      changePrivileges(value){
        console.log(value)
      },
      close() {
        this.$emit('update:show', false)
        this.$emit('update:editId', null)
      },
      //操作
      confirm(form) {
        this.$refs[form].validate((valid) => {
          if (valid) {
            ApiClient.post('/backend/role/txAddRole.do', Object.assign({},this.form,{privilegeIds:this.form.privilegeIds.toString()})).then(() => {
              this.close();
              this.$emit('success')
            })
          } else {
            return false;
          }
        });
      },
      save(form) {
        this.$refs[form].validate((valid) => {
          if (valid) {
            ApiClient.put('/backend/role/txUpdate.do', Object.assign({},this.form,{privilegeIds:this.form.privilegeIds.toString()})).then(() => {
              this.close();
              this.$emit('success')
            })
          } else {
            return false;
          }
        });
      }
    },
    computed: {
      visible: {
        get: function () {
          return this.show == false ? false : true
        },
        set: function () {
          this.$emit('update:show', false)
          this.$emit('update:editId', null)
        }
      }
    },
    mounted() {
      ApiClient.get('/backend/common/privilegesList.do').then(data=>{
        this.privileges = data
        if (this.dataItem) {
          this.form.roleName = this.dataItem.roleName;
          this.form.id = this.dataItem.id;
          if(this.dataItem.privilegeIds === 'all'){
            console.log(this.privileges)
          this.form.privilegeIds = this.privileges.map(function(v){return v.id})
          }else{
            let array = this.dataItem.privilegeIds.split(',')
            this.form.privilegeIds =array.map(function(v){return +v})
          }
        }
      })

    }
  }
</script>
<style lang="scss">
  .add_modle_dialog {
    .el-dialog__header {
      text-align: center;
    }
    .el-form-item {
      margin-bottom: 10px;
    }
    .el-form-item__content {
      text-align: left;
    }
  }
</style>

